const that = this
const data = this.getVariables().find(v => v.name === "screen_params")?.value;
const prefix = this.getVariables().find(v => v.name === 'environmentType')?.value || '';
const eventParams = JSON.parse(data)
const gis = this.getApiWidgetByAliasName('三维组件')
const disposition = this.getApiWidgetByAliasName('处置方案')?.widgetObj;
const eventDetail = this.getApiWidgetByAliasName('事件详情')?.widgetObj;

function baseGet(url) {
  const headers = { "Content-type": "application/json" };
  return fetch(url, { headers }).then(res => res.json())
}

if (eventParams) {
  disposition.setEvent(eventParams);
  disposition.setParams(eventParams.id);
  disposition.initQuery();
  gis.widgetObj.artemis.setEmergencyId(eventParams.id);
  gis.widgetObj.setIconPosition({ right: 570, top: 100 });
  // gis.widgetObj.setModalPosition({ right: 965, bottom: 24, maxWidth: 1910, maxHeight: 960, width: 810, height: 286 })
  gis.widgetObj.setModalPosition({ right: 965, bottom: 24 });
  const $playback = this.getApiWidgetByAliasName('playback');
  const { eventDetails } = eventParams;
  if (Object.keys(eventDetails).length > 0) {
    // 关闭三维弹窗
    gis.widgetObj.gisMap.setModalType('NONE');
    // 回放
    const info = JSON.parse(eventDetails);
    console.log('info', info);
    if (info.jpbPath) {
      $playback.widgetObj.setData([{ data: info?.jpbPath }]);
      $playback.showWidget();
      $playback.widgetObj.render();
    } else {
      $playback.hideWidget();
      window.layer.msg('暂无录像回放')
    }

  } else {
    $playback.hideWidget();
    window.layer.msg('暂无录像回放')
  }

  if (eventParams.sourceDeviceId) {
    // 打开实时视频
    baseGet(`/${prefix}Basics/basics-main/system/equipmentObject/listDetailByEquipmentCode?equipmentCode=${eventParams.sourceDeviceId}`)
      .then(({ success, result } = {}) => {
        if (success && result) {
          const { rows } = result;
          const data = rows[0]?.equipmentConfig?.gbVideoConfig;
          var host = this.getVariables().find(x => x.name == "stream_url").value;
          gis.widgetObj.sullivan.close();
          if (data && data.sipUser && data.videoChannelId) {
            gis.widgetObj.sullivan.addPlayer(`${host}/api/v1/stream/rtc/play`, `${data.sipUser}/${data.videoChannelId}`)
            gis.widgetObj.sullivan.initial()
          } else {
            window.layer.msg('暂无设备号/通道号')
          }
        }
      })
  }else{
    gis.widgetObj.sullivan.close();
    window.layer.msg('未关联摄像头')
  }
}

window.onmessage = function ({ data } = {}) {
  console.log(data);
  if (data.name === 'isOther') {
    that.navigateStage('829716080812359680', [data, data.AuthToken])
  }
};

if (data) {
  let params = {};
  try {
    params = JSON.parse(data)
  } catch (e) {
    console.error(e)
  }

  params.eventPoint = `${params.xCoordinate}，${params.yCoordinate}，${params.zCoordinate}`;

  const $form = this.getApiWidgetByAliasName('judge-content');
  const $img = this.getApiWidgetByAliasName('detail-img');
  const levels = this.getDataById('744574925553008640');
  const types = this.getDataById('744575296061046784');
  const qualitys = this.getDataById('756194853578080256');
  const formData = { ...params };
  // console.log('qualitys', types)

  $form.widgetObj._option.options = {
    eventTypeCode: types?.data,
    eventGradeCode: levels?.data,
    eventQualityCode: qualitys?.data,
    whetherProcess: [
      { name: '未处理', value: 0 },
      { name: '已处理', value: 1 },
      { name: '处理中', value: 2 },
      { name: '已解除', value: 3 },
      { name: '误警', value: 4 },
    ],
  };
  $form.widgetObj._option.dataSource = formData;
  eventDetail._option.dataSource = formData;
  eventDetail.render();
  $form.widgetObj.render();
  const form = this.retrievePlugin('base-forms');
  form.resetForm('form_14');
  form.resetForm('form_20');
  $form.widgetObj.setFormData('form_14', params);

  // 渲染按钮组
  this.getApiWidgetByAliasName('modal-btns')?.widgetObj?.renderDispose();

  try {
    const info = JSON.parse(params.eventDetails);
    if (info.thumbnailPath) {
      const srcArr = info.thumbnailPath.split(',').map(v => ({ src: v }));
      $img.widgetObj.setData([{ data: srcArr }]);
      $img.widgetObj.render()
    } else {
      $img.widgetObj.setData([{ data: [] }]);
      $img.widgetObj.render()
    }
  } catch (e) {
    console.error(e)
  }
}
